package com.yatb.trac.service;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;

import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.io.IOUtils;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;

import com.yatb.trac.pojo.Utenti;

public class TracSetupCreateUserMessagePreparator implements MimeMessagePreparator{

    private String from ;
    private String message;
    private Utenti utente;
	private String url;
    

	public TracSetupCreateUserMessagePreparator(Utenti utente,String url) {
		super();
		this.utente = utente;
		this.url=url;
		
	}


	public void prepare(MimeMessage mimeMessage) throws Exception {
        mimeMessage.setFrom(new InternetAddress(getFrom()));
        mimeMessage.setSubject("SETUP - HRPO");
        mimeMessage.addRecipient(RecipientType.TO, new InternetAddress(this.utente.getEmail()));
        char[] content = IOUtils.toCharArray(new InputStreamReader(TracSetupCreateUserMessagePreparator.class.getResourceAsStream("/com/setup/trac/etc/email_create_user.temp")));
        StringBuilder sb = new StringBuilder();
        sb.append(content);
        Object[] params = new Object[]{utente.getCognome(),utente.getNome(),this.url,utente.getUsername(),utente.getPassword()};
        String message =MessageFormat.format(sb.toString(), params);
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        helper.setText(message);
	}


	public String getFrom() {
		return from;
	}


	public void setFrom(String from) {
		this.from = from;
	}


	public String getMessage() {
		return message;
	}


	public void setMessage(String message) {
		this.message = message;
	}


	public Utenti getUtente() {
		return utente;
	}


	public void setUtente(Utenti utente) {
		this.utente = utente;
	}

}
